23 #define foreach(x, v) for (typeof (v).begin() x=(v).begin(); x !=(v).end(); ++x)
24 #define For(i, a, b) for (int i=(a); i<(b); ++i)
25 #define D(x) cout << #x " is " << x << endl
29 void prevify(string
&s
, int i
);
31 void nextify(string
&s
, int i
) {
34 if (n
== 1) s
[i
] = s
[i
] == '0' ? '1' : '0';
37 if (s
[i
] == '1' and zeros
[i
+1]) s
[i
] = '0'; // 100000000
38 else if (s
[i
] == '0' and s
[i
+1] == '1' and zeros
[i
+2]) s
[i
] = '1'; // 010000000
39 else if (s
[i
] == '0') nextify(s
, i
+ 1);
40 else if (s
[i
] == '1') prevify(s
, i
+ 1);
45 void prevify(string
&s
, int i
) {
48 if (n
== 1) s
[i
] = s
[i
] == '0' ? '1' : '0';
51 if (zeros
[i
]) s
[i
] = '1'; // 0000000000
52 else if (s
[i
] == '1' and s
[i
+1] == '1' and zeros
[i
+2]) s
[i
] = '0'; // 11000000
53 else if (s
[i
] == '0') prevify(s
, i
+ 1);
54 else if (s
[i
] == '1') nextify(s
, i
+ 1);
62 while (cin
>> m
>> s
) {
63 if (m
== 0 and s
== "0") break;
67 for (int i
= n
- 1; i
>= 0; --i
) {
68 zeros
[i
] = zeros
[i
+ 1] and s
[i
] == '0';